/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver.server;

import java.io.Serializable;
import java.util.Date;
import jeeobserver.server.Statistics.DateStatistics;
import jeeobserver.server.Statistics.SamplingStatistics;

/**
 * The Class JvmTotalStatistics.
 *
 * @author Luca Mingardi
 * @version 4.0
 */
public abstract class JvmStatistics extends SamplingStatistics implements Serializable {

	 private static final long serialVersionUID = 40L;

	 private final DateStatistics dateStatistics;

	 private final NumberRangeStatistics cpuNanosStatistics;

	 private final NumberRangeStatistics cpuPercentageStatistics;

	 private final NumberRangeStatistics heapMemoryUsedBytesStatistics;

	 private final NumberRangeStatistics heapMemoryCommittedBytesStatistics;

	 private final NumberRangeStatistics heapMemoryMaxBytesStatistics;

	 private final NumberRangeStatistics nonHeapMemoryUsedBytesStatistics;

	 private final NumberRangeStatistics nonHeapMemoryCommittedBytesStatistics;

	 private final NumberRangeStatistics nonHeapMemoryMaxBytesStatistics;

	 private final NumberRangeStatistics threadsStatistics;

	 private final NumberRangeStatistics processorsStatistics;

	 private final NumberRangeStatistics garbageCollectionsStatistics;

	 private final NumberRangeStatistics garbageCollectionNanosStatistics;

	 private final NumberRangeStatistics loadedClassesStatistics;

	 public JvmStatistics(Date date, int count, DateStatistics dateStatistics, NumberRangeStatistics cpuNanosStatistics, NumberRangeStatistics cpuPercentageStatistics, NumberRangeStatistics heapUsedBytesStatistics, NumberRangeStatistics heapCommittedBytesStatistics, NumberRangeStatistics heapMaxBytesStatistics, NumberRangeStatistics nonHeapUsedBytesStatistics, NumberRangeStatistics nonHeapCommittedBytesStatistics, NumberRangeStatistics nonHeapMaxBytesStatistics, NumberRangeStatistics threadsCountStatistics, NumberRangeStatistics processorsCountStatistics, NumberRangeStatistics garbageCollectionsStatistics, NumberRangeStatistics garbageCollectionNanosStatistics, NumberRangeStatistics loadedClassesStatistics) {
			super(date, count);
			this.dateStatistics = dateStatistics;

			this.cpuNanosStatistics = cpuNanosStatistics;
			this.cpuPercentageStatistics = cpuPercentageStatistics;

			this.heapMemoryUsedBytesStatistics = heapUsedBytesStatistics;
			this.heapMemoryCommittedBytesStatistics = heapCommittedBytesStatistics;
			this.heapMemoryMaxBytesStatistics = heapMaxBytesStatistics;

			this.nonHeapMemoryUsedBytesStatistics = nonHeapUsedBytesStatistics;
			this.nonHeapMemoryCommittedBytesStatistics = nonHeapCommittedBytesStatistics;
			this.nonHeapMemoryMaxBytesStatistics = nonHeapMaxBytesStatistics;

			this.threadsStatistics = threadsCountStatistics;
			this.processorsStatistics = processorsCountStatistics;

			this.garbageCollectionsStatistics = garbageCollectionsStatistics;
			this.garbageCollectionNanosStatistics = garbageCollectionNanosStatistics;

			this.loadedClassesStatistics = loadedClassesStatistics;
	 }

	 public JvmStatistics(Date date, JvmStatistics statistics) {

			this(date, statistics.getCount(),
							statistics.getDateStatistics(),
							statistics.getCpuNanosStatistics(),
							statistics.getCpuPercentageStatistics(),
							statistics.getHeapMemoryUsedBytesStatistics(),
							statistics.getHeapMemoryCommittedBytesStatistics(),
							statistics.getHeapMemoryMaxBytesStatistics(),
							statistics.getNonHeapMemoryUsedBytesStatistics(),
							statistics.getNonHeapMemoryCommittedBytesStatistics(),
							statistics.getNonHeapMemoryMaxBytesStatistics(),
							statistics.getThreadsStatistics(),
							statistics.getProcessorsStatistics(),
							statistics.getGarbageCollectionsStatistics(),
							statistics.getGarbageCollectionNanosStatistics(),
							statistics.getLoadedClassesStatistics());
	 }

	 public void add(JvmStatistics statistics) {

			super.add(statistics.getCount());

			this.cpuNanosStatistics.add(statistics.getCpuNanosStatistics());
			this.cpuPercentageStatistics.add(statistics.getCpuPercentageStatistics());

			this.heapMemoryUsedBytesStatistics.add(statistics.getHeapMemoryUsedBytesStatistics());
			this.heapMemoryCommittedBytesStatistics.add(statistics.getHeapMemoryCommittedBytesStatistics());
			this.heapMemoryMaxBytesStatistics.add(statistics.getHeapMemoryMaxBytesStatistics());

			this.nonHeapMemoryUsedBytesStatistics.add(statistics.getNonHeapMemoryUsedBytesStatistics());
			this.nonHeapMemoryCommittedBytesStatistics.add(statistics.getNonHeapMemoryCommittedBytesStatistics());
			this.nonHeapMemoryMaxBytesStatistics.add(statistics.getNonHeapMemoryMaxBytesStatistics());

			this.threadsStatistics.add(statistics.getThreadsStatistics());
			this.processorsStatistics.add(statistics.getProcessorsStatistics());

			this.loadedClassesStatistics.add(statistics.getLoadedClassesStatistics());

			this.dateStatistics.add(statistics.getDateStatistics());
	 }

	 public DateStatistics getDateStatistics() {
			return dateStatistics;
	 }

	 public NumberRangeStatistics getCpuNanosStatistics() {
			return cpuNanosStatistics;
	 }

	 public NumberRangeStatistics getCpuPercentageStatistics() {
			return cpuPercentageStatistics;
	 }

	 public NumberRangeStatistics getHeapMemoryUsedBytesStatistics() {
			return heapMemoryUsedBytesStatistics;
	 }

	 public NumberRangeStatistics getHeapMemoryCommittedBytesStatistics() {
			return heapMemoryCommittedBytesStatistics;
	 }

	 public NumberRangeStatistics getHeapMemoryMaxBytesStatistics() {
			return heapMemoryMaxBytesStatistics;
	 }

	 public NumberRangeStatistics getNonHeapMemoryUsedBytesStatistics() {
			return nonHeapMemoryUsedBytesStatistics;
	 }

	 public NumberRangeStatistics getNonHeapMemoryCommittedBytesStatistics() {
			return nonHeapMemoryCommittedBytesStatistics;
	 }

	 public NumberRangeStatistics getNonHeapMemoryMaxBytesStatistics() {
			return nonHeapMemoryMaxBytesStatistics;
	 }

	 public NumberRangeStatistics getThreadsStatistics() {
			return threadsStatistics;
	 }

	 public NumberRangeStatistics getProcessorsStatistics() {
			return processorsStatistics;
	 }

	 public NumberRangeStatistics getGarbageCollectionsStatistics() {
			return garbageCollectionsStatistics;
	 }

	 public NumberRangeStatistics getGarbageCollectionNanosStatistics() {
			return garbageCollectionNanosStatistics;
	 }

	 public NumberRangeStatistics getLoadedClassesStatistics() {
			return loadedClassesStatistics;
	 }

	 public static final class TimeStatistics extends JvmStatistics {

			private static final long serialVersionUID = 40L;

			public TimeStatistics(Date date, JvmStatistics statistics) {
				 super(date, statistics);
			}
	 }

	 public static final class TotalStatistics extends JvmStatistics {

			private static final long serialVersionUID = 40L;

			private final int samplingPeriod;

			private final String name;

			private final NumberTrend cpuNanosTrend;

			private final NumberTrend cpuPercentageTrend;

			private final NumberTrend heapMemoryUsedBytesTrend;

			private final NumberTrend heapMemoryCommittedBytesTrend;

			private final NumberTrend heapMemoryMaxBytesTrend;

			private final NumberTrend nonHeapMemoryUsedBytesTrend;

			private final NumberTrend nonHeapMemoryCommittedBytesTrend;

			private final NumberTrend nonHeapMemoryMaxBytesTrend;

			private final NumberTrend threadsTrend;

			private final NumberTrend processorsTrend;

			private final NumberTrend garbageCollectionsTrend;

			private final NumberTrend garbageCollectionNanosTrend;

			private final NumberTrend loadedClassesTrend;

			public TotalStatistics(JvmStatistics statistics, String name, int samplingPeriod) {
				 super(statistics.getDate(), statistics);

				 this.samplingPeriod = samplingPeriod;

				 this.name = name;

				 this.cpuNanosTrend = new NumberTrend();
				 this.cpuNanosTrend.add(statistics.getDate(), statistics.getCpuNanosStatistics().getSum(), statistics.getCount());

				 this.cpuPercentageTrend = new NumberTrend();
				 this.cpuPercentageTrend.add(statistics.getDate(), statistics.getCpuPercentageStatistics().getSum(), statistics.getCount());

				 this.heapMemoryUsedBytesTrend = new NumberTrend();
				 this.heapMemoryUsedBytesTrend.add(statistics.getDate(), statistics.getHeapMemoryUsedBytesStatistics().getSum(), statistics.getCount());
				 this.heapMemoryCommittedBytesTrend = new NumberTrend();
				 this.heapMemoryCommittedBytesTrend.add(statistics.getDate(), statistics.getHeapMemoryCommittedBytesStatistics().getSum(), statistics.getCount());
				 this.heapMemoryMaxBytesTrend = new NumberTrend();
				 this.heapMemoryMaxBytesTrend.add(statistics.getDate(), statistics.getHeapMemoryMaxBytesStatistics().getSum(), statistics.getCount());

				 this.nonHeapMemoryUsedBytesTrend = new NumberTrend();
				 this.nonHeapMemoryUsedBytesTrend.add(statistics.getDate(), statistics.getNonHeapMemoryUsedBytesStatistics().getSum(), statistics.getCount());
				 this.nonHeapMemoryCommittedBytesTrend = new NumberTrend();
				 this.nonHeapMemoryCommittedBytesTrend.add(statistics.getDate(), statistics.getNonHeapMemoryCommittedBytesStatistics().getSum(), statistics.getCount());
				 this.nonHeapMemoryMaxBytesTrend = new NumberTrend();
				 this.nonHeapMemoryMaxBytesTrend.add(statistics.getDate(), statistics.getNonHeapMemoryMaxBytesStatistics().getSum(), statistics.getCount());

				 this.threadsTrend = new NumberTrend();
				 this.threadsTrend.add(statistics.getDate(), statistics.getThreadsStatistics().getSum(), statistics.getCount());
				 this.processorsTrend = new NumberTrend();
				 this.processorsTrend.add(statistics.getDate(), statistics.getProcessorsStatistics().getSum(), statistics.getCount());

				 this.garbageCollectionsTrend = new NumberTrend();
				 this.garbageCollectionsTrend.add(statistics.getDate(), statistics.getGarbageCollectionsStatistics().getSum(), statistics.getCount());
				 this.garbageCollectionNanosTrend = new NumberTrend();
				 this.garbageCollectionNanosTrend.add(statistics.getDate(), statistics.getGarbageCollectionNanosStatistics().getSum(), statistics.getCount());

				 this.loadedClassesTrend = new NumberTrend();
				 this.loadedClassesTrend.add(statistics.getDate(), statistics.getLoadedClassesStatistics().getSum(), statistics.getCount());
			}

			@Override
			public void add(JvmStatistics statistics) {
				 super.add(statistics);

				 this.cpuNanosTrend.add(statistics.getDate(), statistics.getCpuNanosStatistics().getSum(), statistics.getCount());
				 this.cpuPercentageTrend.add(statistics.getDate(), statistics.getCpuPercentageStatistics().getSum(), statistics.getCount());

				 this.heapMemoryUsedBytesTrend.add(statistics.getDate(), statistics.getHeapMemoryUsedBytesStatistics().getSum(), statistics.getCount());
				 this.heapMemoryCommittedBytesTrend.add(statistics.getDate(), statistics.getHeapMemoryCommittedBytesStatistics().getSum(), statistics.getCount());
				 this.heapMemoryMaxBytesTrend.add(statistics.getDate(), statistics.getHeapMemoryMaxBytesStatistics().getSum(), statistics.getCount());

				 this.nonHeapMemoryUsedBytesTrend.add(statistics.getDate(), statistics.getNonHeapMemoryUsedBytesStatistics().getSum(), statistics.getCount());
				 this.nonHeapMemoryCommittedBytesTrend.add(statistics.getDate(), statistics.getNonHeapMemoryCommittedBytesStatistics().getSum(), statistics.getCount());
				 this.nonHeapMemoryMaxBytesTrend.add(statistics.getDate(), statistics.getNonHeapMemoryMaxBytesStatistics().getSum(), statistics.getCount());

				 this.threadsTrend.add(statistics.getDate(), statistics.getThreadsStatistics().getSum(), statistics.getCount());
				 this.processorsTrend.add(statistics.getDate(), statistics.getProcessorsStatistics().getSum(), statistics.getCount());

				 this.garbageCollectionsTrend.add(statistics.getDate(), statistics.getGarbageCollectionsStatistics().getSum(), statistics.getCount());
				 this.garbageCollectionNanosTrend.add(statistics.getDate(), statistics.getGarbageCollectionNanosStatistics().getSum(), statistics.getCount());

				 this.loadedClassesTrend.add(statistics.getDate(), statistics.getLoadedClassesStatistics().getSum(), statistics.getCount());
			}

			public void calculateTrendStatistics() {
				 this.cpuNanosTrend.calculateTrendStatistics();
				 this.cpuPercentageTrend.calculateTrendStatistics();

				 this.heapMemoryUsedBytesTrend.calculateTrendStatistics();
				 this.heapMemoryCommittedBytesTrend.calculateTrendStatistics();
				 this.heapMemoryMaxBytesTrend.calculateTrendStatistics();

				 this.nonHeapMemoryUsedBytesTrend.calculateTrendStatistics();
				 this.nonHeapMemoryCommittedBytesTrend.calculateTrendStatistics();
				 this.nonHeapMemoryMaxBytesTrend.calculateTrendStatistics();

				 this.threadsTrend.calculateTrendStatistics();
				 this.processorsTrend.calculateTrendStatistics();

				 this.garbageCollectionsTrend.calculateTrendStatistics();
				 this.garbageCollectionNanosTrend.calculateTrendStatistics();

				 this.loadedClassesTrend.calculateTrendStatistics();

			}

			public int getSamplingPeriod() {
				 return samplingPeriod;
			}

			public String getName() {
				 return name;
			}

			public NumberTrend getCpuNanosTrend() {
				 return cpuNanosTrend;
			}

			public NumberTrend getCpuPercentageTrend() {
				 return cpuPercentageTrend;
			}

			public NumberTrend getHeapMemoryUsedBytesTrend() {
				 return heapMemoryUsedBytesTrend;
			}

			public NumberTrend getHeapMemoryCommittedBytesTrend() {
				 return heapMemoryCommittedBytesTrend;
			}

			public NumberTrend getHeapMemoryMaxBytesTrend() {
				 return heapMemoryMaxBytesTrend;
			}

			public NumberTrend getNonHeapMemoryUsedBytesTrend() {
				 return nonHeapMemoryUsedBytesTrend;
			}

			public NumberTrend getNonHeapMemoryCommittedBytesTrend() {
				 return nonHeapMemoryCommittedBytesTrend;
			}

			public NumberTrend getNonHeapMemoryMaxBytesTrend() {
				 return nonHeapMemoryMaxBytesTrend;
			}

			public NumberTrend getThreadsTrend() {
				 return threadsTrend;
			}

			public NumberTrend getProcessorsTrend() {
				 return processorsTrend;
			}

			public NumberTrend getGarbageCollectionsTrend() {
				 return garbageCollectionsTrend;
			}

			public NumberTrend getGarbageCollectionNanosTrend() {
				 return garbageCollectionNanosTrend;
			}

			public NumberTrend getLoadedClassesTrend() {
				 return loadedClassesTrend;
			}
	 }

	 public static final class Element extends Statistics.Element {

			private static final long serialVersionUID = 40L;

			private String context;

			public Element(int id, String context) {
				 super(id);
				 this.context = context;
			}

			public Element(String context) {
				 super((context).hashCode());
				 this.context = context;
			}

			/**
			 * Gets the context.
			 *
			 * @return the context
			 */
			public String getContext() {
				 return this.context;
			}
	 }
}
